<?php
/*
 * Created by IntelliJ IDEA.
 * User: Kwesi
 * Date: 4/11/14
 * Time: 2:05 AM
 */

	include("gen.php");
	$cmd=get_datan("cmd");
	switch($cmd)
    {
        case 1:
            //get one vaccine based on id
            get_community();
            break;

        case 2:
            //return a row in an array
            get_all_communities();
            break;

        case 3:
            update_community();
            break;

        case 4:
            add_community();
            break;
        case 5:
            delete_community();
            break;
        default:
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","unknown command");
            echo "}";

    }


	function get_community()
    {
        include_once("community.php");

        $id=get_datan("id");
        $v=new community();
        $row=$v->get_community($id);
        if(!$row){
            echo "{";
            echo jsonn("result",0).",";
            echo jsons("message","community not found");
            echo "}";
            return;
    }

        echo "{";
         echo jsonn("result",1).",";
         echo '"community":{';
         echo jsonn("id",$id).",";

          //name
         echo jsons("community_name",$row['community_name']).",";
         //latitude
         echo jsonn("latitude",$row['latitude']).",";
         //longitude
         echo jsonn("longitude",$row['longitude']).",";
         //population
         echo jsonn("population",$row['population']).",";
         //household
         echo jsonn("household",$row['household']);
         echo "}";
        echo "}";
    }
    function add_community()
    {

        $community_name = get_data('cna');
        $latitude = get_data('cla');
        $longitude = get_data('clg');
        $population = get_datan('cpo');
        $household = get_datan('cho');


        include("community.php");
        $v = new community();
        if(!$v-> add_community($community_name,$latitude,$longitude,$population,
            $household))
        {
            echo '{"result": 0,"message":"adding failed"}';
        }
        else
        {
            echo '{"result": 1,"message":"adding successful"}';
        }
    }

	function update_community()
    {
        $id= get_datan('id');
        $community_name = get_data('cn');
        $latitude = get_data('ct');
        $longitude = get_data('ct');
        $population = get_datan('cp');
        $household= get_datan('ch');

        if(!$id)
        {
            //return error message
            //echo'{"result", 0,"message","id not correct"}';
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","id not correct");
            echo "}";
            return;
        }
        include("community.php");
        $v = new community();
        if(!$v-> update_community($id,$community_name,$latitude,$longitude,$population,$household))
        {
            //echo '{"result", 0,"message","update failed"}';
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","update failed");
            echo "}";
            return;
        }
        else
        {
            //echo '{"result", 1,"message","updated"}';
            echo "{";
            echo jsonn("result",1). ",";
            echo jsons("message","updated");
            echo "}";
        }
    }

    function delete_community()
    {
        $community_id=get_datan('cmid');
        include_once("community.php");
        $v = new community();

        if(!$v->delete_community($community_id))
        {
        //return error message
            echo "{";
            echo jsonn("result",0). ",";
            echo jsons("message","id not correct");
            echo "}";
            return;

        }
    }
?>